{extend name="base" /}
{block name="body"}
<div class="layui-card">
    <table id="tableList" lay-filter="tableList"></table>

    <!-- 表格操作列 -->
    <script type="text/html" id="tableTBTrack">
        {{# if(d.url == '' || d.url == null){ }}
        <a class="layui-btn layui-btn-sm layui-btn-primary ajax-action">查看</a>
        {{# }else{ }}
        <a href="{{d.url}}" class="layui-btn layui-btn-sm">查看</a>
        {{# } }}
        {{# if(d.status == '0'){ }}
        <a href="{:url('store/notice/edit')}?id={{d.id}}&status=1" class="layui-btn layui-btn-sm ajax-action">未读</a>
        {{# }else{ }}
        <a href="{:url('store/notice/edit')}?id={{d.id}}&status=0" class="layui-btn layui-btn-primary layui-btn-sm ajax-action">已读</a>
        {{# } }}
        <a href="{:url('store/notice/del')}?id={{d.id}}" class="layui-btn layui-btn-danger layui-btn-sm ajax-delete">删除</a>
    </script>
    <!-- 权限列 -->
    <script type="text/html" id="type">
        {{# if(d.type == 'track'){ }}
        <a class="layui-btn layui-btn-sm layui-btn-normal">跟进/回访提醒</a>
        {{# }else if(d.type == 'contract'){ }}
        <a class="layui-btn layui-btn-normal layui-btn-sm">合同事项提醒</a>
        {{# }else if(d.type == 'birthday'){ }}
        <a class="layui-btn layui-btn-normal layui-btn-sm">生日提醒</a>
        {{# }else if(d.type == 'other'){ }}
        <a class="layui-btn layui-btn-normal layui-btn-sm">其他提醒</a>
        {{# } }}
    </script>
    <!-- 权限列 -->
    <script type="text/html" id="customer">
        {{# if(d.customer_id != '0'){ }}
        <a href="{:url('store/customer/detail')}?id={{d.customer_id}}" class="layui-btn layui-btn-primary layui-btn-sm">{{d.customer.name}}</a>
        {{#  } }}
    </script>
</div>
{/block}

{block name="js"}
<script>
    layui.use(['layer', 'form', 'table', 'util', 'dropdown'], function () {

        var table = layui.table;
        // 渲染回访表格
        var insTb = table.render({
            elem: '#tableList',
            url: "{:url('store/notice/index_json')}",
            toolbar: ['<p>',
                '<a href="{:url('store/notice/add')}" class="layui-btn layui-btn-sm ajax-iframe" data-width="650px" data-height="100%"><i class="layui-icon">&#xe61f;</i>新增提醒</a>',
                '<button lay-event="read" class="layui-btn layui-btn-sm layui-btn-normal"><i class="layui-icon">&#xe609;</i>批量已读</button>',
                '<button lay-event="unread" class="layui-btn layui-btn-sm layui-btn-normal"><i class="layui-icon">&#xe609;</i>批量未读</button>',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger"><i class="layui-icon">&#xe640;</i>批量删除</button>',
            '</p>'].join(''),
            page: true,
            cellMinWidth: 100,
            limit:'15',
            limits:[15,30,50,100,200,500,1000],
            size:'lg',even:true,
            cols: [[
                {type:'checkbox',title:'#'},
                {field: 'datetime', align: 'center', sort: true, title: '消息时间',width: 140},
                {templet: '#type', align: 'left', sort: true, title: '类型',width:200},
                {field: 'remark', align: 'left', sort: true, title: '提醒事项'},
                {field: 'create_time', align: 'center', sort: true, title: '创建时间',width: 200},
                {align: 'center', toolbar: '#tableTBTrack', title: '操作', width: 200}
            ]],
            parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            }
        });
        /* 表格头工具栏点击事件 */
        table.on('toolbar(tableList)', function (obj) {
            //console.log(obj)
            if(obj.event === 'read') { // 转移
                var checkRows = table.checkStatus('tableList');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要设为已读的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                console.log(ids);
                layer.confirm('确定批量设置已读？', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    var index = layer.msg('操作中，请稍候', {
                        icon: 16,
                        time: false,
                        shade: 0.3
                    });
                    $.ajax({
                        url: "{:url('store/notice/read')}",
                        type: 'post',
                        dataType: 'json',
                        data:{
                            id:ids
                        },
                        success: function (result) {
                            layer.msg(result.msg);
                            setTimeout(function () {
                                location.reload();
                            }, 1000);
                        },
                    });
                });
            }else if(obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('tableList');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                layer.confirm('确定批量领删除？', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    var index = layer.msg('操作中，请稍候', {
                        icon: 16,
                        time: false,
                        shade: 0.3
                    });
                    $.ajax({
                        url: "{:url('store/notice/del')}",
                        type: 'post',
                        dataType: 'json',
                        data:{
                            id:ids
                        },
                        success: function (result) {
                            layer.msg(result.msg);
                            setTimeout(function () {
                                location.reload();
                            }, 1000);
                        },
                    });
                });
            }else if(obj.event === 'unread') { // 删除
                var checkRows = table.checkStatus('tableList');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要设为未读的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                layer.confirm('确定批量设置未读？', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    var index = layer.msg('操作中，请稍候', {
                        icon: 16,
                        time: false,
                        shade: 0.3
                    });
                    $.ajax({
                        url: "{:url('store/notice/unread')}",
                        type: 'post',
                        dataType: 'json',
                        data:{
                            id:ids
                        },
                        success: function (result) {
                            layer.msg(result.msg);
                            setTimeout(function () {
                                location.reload();
                            }, 1000);
                        },
                    });
                });
            }
        });
    });

</script>
{/block}